package com.ym.manager.admin.subscribe;


import com.ym.manager.admin.act.ActService;
import com.ym.manager.admin.act.model.Act;
import com.ym.manager.sys.model.AdminUser;
import com.ym.utils.DateUtil;
import com.ym.utils.ResultUtil;
import com.ym.web.bind.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * 预约活动
 */
@Controller
@RequestMapping("/subscribe/")
public class SubscribeController {

    @Autowired
    private SubscribeService subscribeService;


    /**
     * 活动列表
     * @return
     */
    @RequestMapping("list")
    public String list(){
        return "/subscribe/list";
    }

    /**
     * 页面：新增活动
     * @return
     */
    @GetMapping("add")
    public String addSubscribe(){
        return "/subscribe/add";
    }

    @PostMapping("add")
    @ResponseBody
    public String add(Act act,@CurrentUser AdminUser adminUser){
        Map<String,Object> result = ResultUtil.result();
        act.setMerchant_id(adminUser.getMerchant_id());
        act.setCreate_time(DateUtil.getDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        try {
            int i;
            if(act.getId()>0){
                i = subscribeService.update(act);
                result.put("code",i>0?0:-1);
                result.put("msg",i>0?"修改成功":"修改失败，请联系管理员");
            }else{
                i = subscribeService.insert(act);
                result.put("code",i>0?0:-1);
                result.put("msg",i>0?"新增成功":"新增失败，请联系管理员");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }


    /**
     * @ 名称:修改活动
     * @ 创建人: zhangmijia
     * @ 创建时间: 23:23
     * @ 修改人和其它信息
     * @ 参数：
     * @ 返回值:
     */
    @GetMapping("edit")
    public String editSubscribe(Model model,int id){
        model.addAttribute("subscribe",subscribeService.detail(id));
        return "/subscribe/edit";
    }

}
